<?php
/**
 * description...
 * 
 * @author Dvaknheo
 * @category DnToolkit
 * @copyright Copyright(c) 2012 
 * @version $Id$
 */

/**
 * APP 基类
 */
class DnAppBase implements IDnApp
{
	public $_mode;
	public $_service; //DnService;
	public $last_error;
	
	public function __construct()
	{
		$this->_service=new DnService($this);
	}
	public function __desruct()
	{
		unset($this->_service);
	}
	
	public static function Mode($mode,$me=null)
	{
		$me->_mode=$mode;
		return $me->_service;
	}
	
	/**
	 * for IDnAmf
	 */
	public static function DnAmfClassSelfCreate($thisClassName)
	{
		return call_user_func(array($thisClassName,'Run'));
	}
	//////////////////////////////////////////////////////////////////////////
    /**
     * @implements IDnApp
     * 
     * @access public
     * @param unknown $method
     * @return unknown
     */
	public function onPreCall($method)
	{
		return true;
	}
    /**
     * @implements IDnApp
     * 
     * @access public
     * @param unknown $ret
     * @param unknown $method
     * @return unknown
     */
	public function onPostCall($ret,$method)
	{
		if (defined('DN_IN_AMF')) { $this->_mode='amf';	}
		switch($this->_mode){
			case 'amf':
				$ret=array('ret'=>$ret,'msg'=>'','amf'=>true);
				break;
			case 'json':
				$ret=array('ret'=>$ret,'msg'=>'');
				break;
			default:
		}
		$this->mode=null;
		return $ret;
		
	}
    /**
	 * Don't call In Amf
     * @implements IDnApp;
     * 
     * @access public
     * @param unknown $ex
     * @param unknown $is_service
     * @return unknown
     */
	public function onError($ex,$is_service)
	{
		$msg=$ex->getMessage();
		$mode=$this->_mode;
		switch($this->_mode){
			case 'json':
				$ret=array('ret'=>null,'msg'=>$msg);
				break;
			default:
				
		}
		$this->_mode=null;
		if (defined('DN_IN_AMF')) {
			$ret=array('ret'=>null,'msg'=>$msg);
			return $ret;
		}
		$this->setLastError($msg);
		return null;
	}
	public function getLastError()
	{
		return $this->last_error;
	}
	protected function setLastError($error)
	{
		$this->last_error=$error;
	}
}
